package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.data.enums.ResponseEnum;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.forattitude.TypeEnumForAttitude;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.Attitude;
import com.jilefojing.beans.pojos.Statistics;
import com.jilefojing.beans.pojos.enhanced.EnhancedAttitude;
import com.jilefojing.beans.query.AttitudeQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IAttitudeDao;
import com.jilefojing.dao.IStatisticsDao;
import com.jilefojing.service.IAttitudeService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月22日 23时23分37秒
 */
@Service
public class AttitudeService extends BaseService<Attitude> implements IAttitudeService {
	@Autowired
	private IAttitudeDao attitudeDao;
	@Autowired
	private IStatisticsDao statisticsDao;
	@Autowired
	private IStatisticsService statisticsService;

	@Override
	public EnhancedAttitude getEnhancedAttitudesByItemForUser(ItemTypeEnumForSource itemType, long itemId, TypeEnumForAttitude type, UserContext userContext) {
		List<EnhancedAttitude> enhancedAttitudes = this.getEnhancedAttitudesByItemsForUser(itemType, Arrays.asList(new Long[] { itemId }), type, userContext);
		if (!Detect.notEmpty(enhancedAttitudes)) {
			return null;
		}
		return enhancedAttitudes.get(0);
	}

	@Override
	public List<EnhancedAttitude> getEnhancedAttitudesByItemsForUser(ItemTypeEnumForSource itemType, List<Long> itemIds, TypeEnumForAttitude type, UserContext userContext) {
		AttitudeQuery attitudeQuery = new AttitudeQuery();
		attitudeQuery.setItemTypes(Arrays.asList(new ItemTypeEnumForSource[] { itemType }));
		attitudeQuery.setItemIds(itemIds);
		attitudeQuery.setTypes(Arrays.asList(new TypeEnumForAttitude[] { type }));
		attitudeQuery.setUserIds(Arrays.asList(new Long[] { userContext.getEnhancedUser().getId() }));

		Pagination<Attitude> pagination = new Pagination<Attitude>(1, Integer.MAX_VALUE, false);
		pagination = attitudeDao.getPagination(attitudeQuery, pagination);
		if (!Detect.notEmpty(pagination.getItems())) {
			return null;
		}
		List<EnhancedAttitude> enhancedAttitudes = this.conventAttitudes2EnhancedAttitudes(pagination.getItems());
		return enhancedAttitudes;
	}

	@Override
	public List<EnhancedAttitude> conventAttitudes2EnhancedAttitudes(List<Attitude> attitudes) {
		List<EnhancedAttitude> enhancedAttitudes = new ArrayList<EnhancedAttitude>();
		for (Attitude _Attitude : attitudes) {
			enhancedAttitudes.add(new EnhancedAttitude(_Attitude));
		}
		return enhancedAttitudes;
	}

	@Override
	public Attitude add(Attitude attitude, UserContext userContext) throws UserContextException {
		if (attitude.getItemId() == null || attitude.getItemType() == null || attitude.getType() == null || userContext.getUserId() < 1) {
			throw new UserContextException("缺少必要参数");
		}
		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByCode(attitude.getItemType());
		TypeEnumForAttitude type = TypeEnumForAttitude.getTypeEnumByCode(attitude.getType());
		EnhancedAttitude enhancedAttitude = this.getEnhancedAttitudesByItemForUser(itemType, attitude.getItemId(), type, userContext);
		if (enhancedAttitude != null) {
			if (enhancedAttitude.getType() != type) {
				throw new UserContextException(ResponseEnum.ATTITUDE_EXIST_OTHER_TYPE);
			} else {
				throw new UserContextException(ResponseEnum.ATTITUDE_TYPE_EXIST);
			}
		}
		Date now = new Date();

		Attitude record = new Attitude();
		record.setItemId(attitude.getItemId());
		record.setItemType(attitude.getItemType());
		record.setType(attitude.getType());
		record.setUserId(userContext.getUserId());
		record.setCreateTime(now);

		attitudeDao.saveSelective(record);

		if (record.getType() == TypeEnumForAttitude.PRAISE.getCode()) {
			statisticsService.updateCount(itemType, record.getItemId(), Statistics.COLUMN_COUNT_PRAISE, true, 1, userContext);
		} else {
			throw new UserContextException("需增加统计类型");
		}

		return record;
	}

	@Override
	public void cancle(Attitude attitude, UserContext userContext) throws UserContextException {
		if (attitude.getItemId() == null || attitude.getItemType() == null || attitude.getType() == null || userContext.getUserId() < 1) {
			throw new UserContextException("缺少必要参数");
		}
		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByCode(attitude.getItemType());
		TypeEnumForAttitude type = TypeEnumForAttitude.getTypeEnumByCode(attitude.getType());
		EnhancedAttitude enhancedAttitude = this.getEnhancedAttitudesByItemForUser(itemType, attitude.getItemId(), type, userContext);
		if (enhancedAttitude == null) {
			throw new UserContextException(ResponseEnum.ATTITUDE_NOT_EXIST);
		}

		if (enhancedAttitude.getType() != type) {
			throw new UserContextException(ResponseEnum.ATTITUDE_EXIST_OTHER_TYPE);
		}

		attitudeDao.deleteById(enhancedAttitude.getId());

		statisticsService.updateCount(itemType, attitude.getItemId(), Statistics.COLUMN_COUNT_PRAISE, false, 1, userContext);
	}
}
